package com.shrcl.engineerprocess.domain.impl;

import com.shrcl.engineerprocess.domain.EngineerProcessSequenceService;
import com.shrcl.engineerprocess.domain.EngineerSeqConfig;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.utils.DateUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Service;

@Service("com.shrcl.engineerprocess.domain.EngineerProcessSequenceService")
public class EngineerProcessSequenceServiceImpl extends BaseService implements EngineerProcessSequenceService {
  public String getNextWorkOrderSequence() {
    String _xblockexpression = null;
    {
      final EngineerSeqConfig config = this.computeConfig();
      Integer _engineerWorkOrderSequence = config.getEngineerWorkOrderSequence();
      Integer _nullsafe = NumberExtensions.nullsafe(_engineerWorkOrderSequence);
      int _plus = ((_nullsafe).intValue() + 1);
      config.setEngineerWorkOrderSequence(Integer.valueOf(_plus));
      SimpleDateFormat _simpleDateFormat = new SimpleDateFormat(
        "yyyyMMdd");
      final SimpleDateFormat format = _simpleDateFormat;
      Date _sequenceDate = config.getSequenceDate();
      String _format = format.format(_sequenceDate);
      String _plus_1 = ("GD" + _format);
      Integer _engineerWorkOrderSequence_1 = config.getEngineerWorkOrderSequence();
      String _format_1 = String.format("%03d", _engineerWorkOrderSequence_1);
      String _plus_2 = (_plus_1 + _format_1);
      _xblockexpression = (_plus_2);
    }
    return _xblockexpression;
  }
  
  public String getNextEngineerProjectJobSequence() {
    String _xblockexpression = null;
    {
      final EngineerSeqConfig config = this.computeConfig();
      Integer _engineerProjectJobSequence = config.getEngineerProjectJobSequence();
      Integer _nullsafe = NumberExtensions.nullsafe(_engineerProjectJobSequence);
      int _plus = ((_nullsafe).intValue() + 1);
      config.setEngineerProjectJobSequence(Integer.valueOf(_plus));
      SimpleDateFormat _simpleDateFormat = new SimpleDateFormat(
        "yyyyMMdd");
      final SimpleDateFormat format = _simpleDateFormat;
      Date _sequenceDate = config.getSequenceDate();
      String _format = format.format(_sequenceDate);
      String _plus_1 = ("PG" + _format);
      Integer _engineerProjectJobSequence_1 = config.getEngineerProjectJobSequence();
      String _format_1 = String.format("%03d", _engineerProjectJobSequence_1);
      String _plus_2 = (_plus_1 + _format_1);
      _xblockexpression = (_plus_2);
    }
    return _xblockexpression;
  }
  
  public EngineerSeqConfig computeConfig() {
    EngineerSeqConfig _xblockexpression = null;
    {
      Date today = DateUtil.today();IGenericQuery<EngineerSeqConfig> query=getObjectFactory().createGenericQuery(EngineerSeqConfig.class,"select config from com.shrcl.engineerprocess.domain.impl.EngineerSeqConfigImpl as config   where  config.active = 1      ");
      EngineerSeqConfig config = query.uniqueResult();
      Date _sequenceDate = config.getSequenceDate();
      boolean _equals = today.equals(_sequenceDate);
      boolean _not = (!_equals);
      if (_not) {
        config.setSequenceDate(today);
        config.setEngineerWorkOrderSequence(Integer.valueOf(0));
        config.setEngineerProjectJobSequence(Integer.valueOf(0));
      }
      _xblockexpression = (config);
    }
    return _xblockexpression;
  }
}
